package cn.zhouchao.org.controller;

import cn.zhouchao.basic.anno.PreAuthorize;
import cn.zhouchao.basic.util.AjaxResult;
import cn.zhouchao.basic.util.PageList;
import cn.zhouchao.org.aop.DepartmentLogAop;
import cn.zhouchao.org.domain.Department;
import cn.zhouchao.org.domain.Employee;
import cn.zhouchao.org.query.DepartmentQuery;
import cn.zhouchao.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
@PreAuthorize
public class DepartmentController {
    /**
     * 引入Service接口
     */
    @Autowired
    private IDepartmentService departmentService;

    /**
     * Restful 风格 路径变量
     * http://localhost:8080/dept
     * 查询所有
     *
     * @return
     */
    @GetMapping()
    @ApiOperation(value = "查询所有" )
    @DepartmentLogAop(methodName = "查询所有部门", methodType = "findAll")
//    @PreAuthorize(name = "return",sn="dept:List")
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    /**
     * Restful 风格 路径变量
     * http://localhost:80/dept/1
     * 根据id查询一个
     *
     */

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @DepartmentLogAop(methodName = "查询单个部门", methodType = "findById")
//    @PreAuthorize(name = "查询一个",sn="dept:findById")
    public Department findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id) {
        return departmentService.findById(id);
    }

    /**
     * 添加或者修改
     *
     * @param department
     * @return
     */

    @PutMapping()
    @ApiOperation(value = "新增/修改" )
    @DepartmentLogAop(methodName = "新增/修改", methodType = "addOrUpdate")
//    @PreAuthorize(name = "部门添加或者修改",sn="dept:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        if (null != department.getId()) {
            // 修改
            departmentService.update(department);
        } else {
            // 添加
            departmentService.add(department);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除
     *
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @DepartmentLogAop(methodName = "部门删除", methodType = "delete")
    @PreAuthorize(name = "部门删除",sn="dept:delete")
    public AjaxResult delete(@PathVariable("id") Long id) {
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     *批量删除
     *
     */
    @PatchMapping()
    @DepartmentLogAop(methodName = "部门批量删除", methodType = "batchDelete")
//    @PreAuthorize(name = "部门批量删除",sn="dept:batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        departmentService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    /**
     * 分页查询
     */
    @PostMapping()
    @ApiOperation(value = "分页查询" )
    @PreAuthorize(name = "部门分页查询",sn="dept:querylist")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery departmentQuery) {
        PageList<Department> departmentPageList = departmentService.queryByPage(departmentQuery);
        return departmentPageList;
    }

    /**
     * 无限级树
     */
    @GetMapping("/tree")
    @PreAuthorize(name = "部门无限级树",sn="dept:tree")
    public List<Department> tree() {
        return departmentService.tree();
    }

    /*
     * 抽屉：展示点击部门的所对应的员工数据
     *
     * */
    @GetMapping("/i/{id}")
    @ApiOperation(value = "通过ID查询" )
    public List<Employee> findByIdEmp(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id) {
        List<Employee> all = departmentService.findByIdEmp(id);
        all.forEach(System.out::println);
        return departmentService.findByIdEmp(id);

    }
}
